import mixin from "ramda/src/mergeDeepRight";
import {Cesium, wutu3d, AppGlobalConfig, PUBLIC_URL} from "@/public/global/const";

export function initViewer(containerId, ignoreExt, option = {}) {
    option = mixin(
        {
            homeButton: false,
            navigationHelpButton: false,
            baseLayerPicker: true,
            fullscreenButton: false,
            sceneModePicker: false,
            disableZoom: false,
        },
        option,
    );
    const viewer = new Cesium.Viewer(containerId, {
        animation: false,
        timeline: false,
        fullscreenButton: option.fullscreenButton,
        vrButton: false,
        geocoder: false,
        sceneModePicker: option.sceneModePicker,
        homeButton: option.homeButton,
        navigationHelpButton: option.navigationHelpButton,
        baseLayerPicker: option.baseLayerPicker,
        // clockViewModel: getClockViewModel("11:00:00"),
        // imageryProvider: getImageryProviderArr(),
        imageryProviderViewModels:
            AppGlobalConfig.OfflineImageProvider && AppGlobalConfig.OfflineImageProvider.length && getImageryProviderArr(),
        selectionIndicator: true,
        imageryProvider:
            AppGlobalConfig.imageryProvider &&
            new Cesium.createTileMapServiceImageryProvider({
                url: AppGlobalConfig.imageryProvider,
                fileExtension: "jpg",
            }),
    });
    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

    // viewer.scene.logarithmicDepthBuffer = false;
    viewer.scene.globe.enableLighting = true;
    // viewer.scene.sun.show = false;
    viewer.scene.moon.show = false;

    // viewer.scene.skyBox = null;
    if (!ignoreExt) {
        viewer.wutu = new wutu3d.ViewerEx(viewer, {
            contextmenu: true,
            center: option.center || AppGlobalConfig.ViewerHomeCenter,
            mouseZoom: true,
            contextmenuItems: [],
        });
    }

    if (!option.disableZoom) {
        const options = {};
        options.enableCompass = true; //罗盘
        options.enableZoomControls = true; //缩放
        options.enableDistanceLegend = true; //比例尺
        options.enableCompassOuterRing = true; //指南针外环
        viewer.extend(Cesium.viewerCesiumNavigationMixin, options);
    }
    return viewer;
}

/**
 * 获取影像图列表
 */
export function getImageryProviderArr() {
    const providerViewModels = [];
    let imgModel;

    const { OfflineImageProvider } = AppGlobalConfig;
    if (OfflineImageProvider && OfflineImageProvider.length > 0) {
        const wgs84 = new Cesium.GeographicTilingScheme({
            ellipsoid: Cesium.Ellipsoid.WGS84,
        });
        OfflineImageProvider.forEach((provider) => {
            imgModel = new Cesium.ProviderViewModel({
                name: provider.name,
                tooltip: provider.name,
                iconUrl: PUBLIC_URL + "images/basemaps/tdt_img.png",
                creationFunction: function() {
                    const obj = new Cesium.UrlTemplateImageryProvider({
                        url: provider.url,
                        tilingScheme: wgs84,
                        minimumLevel: 1,
                        maximumLevel: 18,
                    });
                    obj.defaultContrast = 0.7;
                    obj.defaultSaturation = 0.5;
                    return obj;
                },
            });
            providerViewModels.push(imgModel);
        });
    }
    return providerViewModels;
}
